/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	hey 
 * 
 * <p>描   述  :   LawInformOpenServiceImpl.java for com.pengtu.gsj.service.impl.lz    </p>
 * 
 * <p>最后修改 : $: Sep 8, 2017-4:12:45 PM v 1.0.0	 hey   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.lzgl;

import java.util.List;


import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.lzgl.LawInformOpenDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.lzgl.LawInformOpen;
import com.pengtu.gsj.service.lzgl.LawInformOpenService;
import com.pengtu.gsj.service.system.SystemManageService;

/**
 * 
 * LawInformOpenServiceImpl
 * 
 * Sep 8, 2017 4:12:45 PM
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class LawInformOpenServiceImpl implements LawInformOpenService{
	@Autowired
	private LawInformOpenDao lawInformOpenDao;
	
	@Autowired
	private SystemManageService systemManageService;
	
	
	
	@Override
	public void saveLawInformOpen(LawInformOpen lawInformOpen){
		lawInformOpenDao.save(lawInformOpen);
	}
	
	@Override
	public Page<LawInformOpen> findLawInformOpen(Page<LawInformOpen> page, final QueryFilter queryFilter){
		return lawInformOpenDao.findPage(page, queryFilter);
	}
	
	@Override
	public LawInformOpen getLawInformOpenById(String id){
		return lawInformOpenDao.get(id);
	}
	
	@Override
	public void  delateLawInformOpen(LawInformOpen lawInformOpen){
		if(lawInformOpen.getAttachments() != null){
			for(Attachment attachment : lawInformOpen.getAttachments()){
				systemManageService.deleteAttachment(attachment);
			}
		}
		lawInformOpenDao.delete(lawInformOpen);
	}
	
	@Override
	public List<LawInformOpen> getLawInformOpen(QueryFilter queryFilter){
		return lawInformOpenDao.find(queryFilter);
	}
	

	/**
	 * 适用：验证执法信息公开中 年份+机构名称 唯一
	 * @param testYear 年份
	 * @param organizationName 机构名称
	 * @param id id
	 */
	@Override
	public boolean checkYearOrg(String testYear, String organizationName, String id) {
		//1、如果id不为空的话，如果是不修改（或修改前后一样）返回true，否则按新增操作处理
		if (StringUtils.isNotEmpty(id)) {
			String oldYear = lawInformOpenDao.get(id).getTestYear();
			String name = lawInformOpenDao.get(id).getOrganizationName();
			if (oldYear.equals(testYear) && name.equals(organizationName)) {
				return true;
			} else {
				return checkSame(testYear, organizationName);
			}
		} else {
			//2、如果id为空时，则用判断方式判断是否存在year的同时存在stationName
			return checkSame(testYear, organizationName);
		}
	}
	
	public Boolean checkSame(String testYear, String organizationName){
		Boolean flag = true;
		if (StringUtils.isEmpty(testYear) || StringUtils.isEmpty(organizationName)) {
			return true;
		} else {
			List<LawInformOpen> govLists = lawInformOpenDao.findBy("testYear", testYear);
			for (LawInformOpen lawInformOpen : govLists) {
				if (flag == true) {
					flag = organizationName.equals(lawInformOpen.getOrganizationName()) ? false : true;
				}
			}
		}
		return flag;
	}
}
